//
//  BaseSwift.swift
//  Project_Demo
//
//  Created by caoxiaozhu on 2018/8/6.
//  Copyright © 2018年 caoxiaozhu. All rights reserved.
//

import UIKit

class BaseSwift: UIViewController {
    
    var a : Int  = 1
    let b : Int = 2
    var c : Int = 0
    var sum: String? = nil
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
//        test1()
//        test2()
//        test3(x: nil, y: 2)
//        test4()
//        test5()
//        test6(num: 100)
//        test7()
//        test8()
//        test9()
//        test10()
//        test11()
        test12()
//        test13()
//        test14()
    }
}

extension BaseSwift{
    //变量常量
    func test1(){
        //TODO: 变量常量
        //        let a = 1
        //        var b = 2
        c = a+b
        sum = "\(c)"
        print(sum!)
    }
    
    //TODO: 控制流
    func test2(){
        //if
        let x = 10
        //简单的If
        //        if x > 5 {
        //            print("大了")
        //        }else{
        //            print("小了")
        //        }
        //三目
        x>5 ? print("三目----大了") : print("三目----小了")
    }
    
    //TODO: ??
    func test3(x : Int? , y : Int?){
        //??后面跟默认值
        print((x ?? 0) + (y ?? 0))
    }
    
    //TODO: if let/var
    func test4(){
        let oName : String? = "老王"
        let oAge : Int? = 10
        
        if let name = oName, let age = oAge {
            print(name + String(age))
        }
    }
    
    //TODO: guard let/var
    func test5(){
        let oName : String? = "老王"
        let oAge : Int? = 10
        
        guard let name = oName, let age = oAge else {
            return print("名字或者年龄为空")
        }
        //跟if let相反，如果为空，则走guard里面的代码，不再执行后面的代码了,比if let降低了分支逻辑,guard 是swift专用代码
        print(name + String(age))
        }
    
    //TODO: Switch
    //在swift中可以用任何值作为分支，而且不需要break，如果要多值就用“，”
    func test6(num: Int){
        switch num {
        case 100:
            print("优秀")
        case 80:
            print("良")
        case 60:
            print("中")
        case 40:
            print("差")
        default:
            print("分都没有")
        }
    }
    
        //TODO: for循环
        //传统的for已经被取消
        func test7(){
            //开区间[0,5),代表0到4
            for i in 0..<5 {
                print("这是\(i)")
            }
            
            print("--------------")
            
            //闭区间[0,5]，代表0-5
            for i in 0...5 {
                print("这是\(i)")
            }
            
            print("--------------")
            //反转
            for i in (0..<10).reversed() {
                print("这是\(i)")
            }
    }
    
    //TODO: 字符串遍历
    func test8(){
        let string = "我要飞得更高"
        //字符串遍历
        for i in string {
            print("\(i)")
        }
        //字符串长度
        print(string.utf8.count)
        print(string.utf16.count)
        print(string.count)
        
        //类型转换string -> NSString
        let ocStr = string as NSString
        print(ocStr.length)
    }
    
    //TODO: 字符串拼接
    func test9(){
        let name = "老王"
        let age = 18
        //可能会出现可选项的问题
        let boss : String? = "BOSS"
        
        let detail = "\(name)\(age) \(boss ?? "")"
        print(detail)
    }
    
    //TODO: 字符串的子串
    func test10(){
        let str = "老王今天去了动物园"
        let indexStartOfText = str.index(str.startIndex, offsetBy: 3)
        let indexEndofText = str.index(str.endIndex, offsetBy: -3)
        let subString1 = str[indexStartOfText...]
        let subString2 = str[..<indexEndofText]
        let subString3 = str[indexStartOfText..<indexEndofText]
        
        print(subString1)
        print(subString2)
        print(subString3)
        
        //字符串替换
        let idStr = "420106195704224321"
        let indexOfStart = idStr.index(idStr.startIndex, offsetBy: 4)
        let indexOfEnd = idStr.index(idStr.endIndex, offsetBy: -4)
        let hideIDStr = idStr.replacingCharacters(in: indexOfStart...indexOfEnd, with: "***********")
        print(hideIDStr)
    }
    
    //TODO: 数组
    func test11(){
        //Swift中数组可以是多种值的，例如string
        var stringArr = ["张三","李四","王五"]
        print(stringArr)
        
        let numArr = [1,2,3]
        print(numArr)
        
        //遍历
        for i in numArr {
            print(i)
        }
        
        //根据下标来遍历
        for i in numArr.enumerated() {
            print("\(i.offset) \(i.element)")
        }
        
        //数组中添加元素
        stringArr.append("老李哦")
        print("添加后的一个值\(stringArr)")
        
        //根据下标来确定改值
        stringArr[1] = "王波波"
        print("改变第一个值\(stringArr)")
        
        //删除
        stringArr.remove(at: 1)
        print("删除后的数组\(stringArr)")
        
        //数组的合并(要合并的数组类型必须一致)
        let stringArr2 = ["我们的爱","过了就不再回来","难道你也是GAY"]
        stringArr += stringArr2
        print("合并后的arr\(stringArr)")
        
    }
    
    //TODO: 字典
    func test12(){
        
        //定义Swfit中也是用[]来定义字典
        var dict = ["name":"cao","age":"18"]
        print("初始化字典==>\(dict)")
        
        //一般开始会规定字典
        let array : [[String : Any]] = [
            ["name":"cao","age":18],
            ["name":"wang","age":19]
        ]
        
        print("特定值的数组==>\(array)")
        
        //增
        dict["sex"] = "male"
        print("增加后的字典==>\(dict)")
        
        //删
        dict.removeValue(forKey: "name")
        print("删除后的字典==>\(dict)")
        
        //改
        dict["name"] = "wang"
        print("修改后的字典==>\(dict)")
        
        print("---------------------------------")
        
        //遍历
        let dict2 = ["name":"cao","age":"18","sex":"male","degree":"master"]
        for (key,value) in dict2 {
            print("第一种方法遍历字典==>\(key),\(value)")
        }
        
        print("---------------------------------")
        
        for e in dict2 {
            print("第二种方法遍历字典==>\(e.key),\(e.value)")
        }
        
        print("---------------------------------")
        
        //合并
        for e in dict2 {
            dict[e.key] = dict2[e.key]
        }
        print("字典合并==>\(dict)")
    }
}

